package com.iss.supermarket.util.network;

import com.iss.supermarket.EntryUI.EntryUI;
import com.iss.supermarket.GoodsInfoManagerUI.GoodsInfoManagerUI;
import com.iss.supermarket.OutFlowUI.OutFlowUI;
import com.iss.supermarket.TradeUI.TradeUI;
import com.iss.udp.UdpThread;
import org.apache.log4j.Logger;

public class NetWork {
    private final Logger logger = Logger.getLogger("NetWork");
    public String goodBarCode;
    public static NetWork nw;

    public NetWork(String ClassName){
        UdpThread udpThread = new UdpThread();
        udpThread.start();

        //传递扫描得到的商品条形码
        udpThread.setOnReceiveMsgLisener(new UdpThread.ReceiveMsgLisener() {
            @Override
            public void receiveMsg(String contents) {
                goodBarCode = contents;
                logger.info(contents);
                if("TradeUI".equals(ClassName)){
                    TradeUI.getTradeInfoUI().setScanningResults(contents);
                }else if("GoodsInfoManagerUI".equals(ClassName)){
                    GoodsInfoManagerUI.getGoodsInfoManageUI().setScanningResults(contents);
                }else if("OutFlowUI".equals(ClassName)){
                    OutFlowUI.getOutFlowUI().setScanningResults(contents);
                }else if("EntryUI".equals(ClassName)){
                    EntryUI.getEntryUI().setScanningResults(contents);
                }
            }
        });
    }

    public static NetWork getNetWork(String ClassName){
        if(nw == null)
            nw = new NetWork(ClassName);
        return nw;
    }
}